<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Forum Generation File
 * File: generateforums_class.php
 * Purpose: For generating forums on the index and viewforum pages
 * Todo: Nothing at the moment
 *
 */

class generate_forums extends LanguageCoreLite {
	private $type;
	private $forum_id;
	private $stats_data;
	private $lates_data;
	private $index_data;
	private $stats_array;
	private $latest_array;
	private $extra_data;
	public $total_forums;
	
	/**
	* Sets data type
	*
	* @param string $type
	* @param int $forum_id
	* @access public
	*/
	public function set_data($type, $forum_id = NULL) {
		// add some variables
		$this->type = $type;
		$this->forum_id = $forum_id;
		
		// include the language file
		self::add_file('main_index');
	}
	
	/**
	* Gets the forum index data from the database and sets it as an array
	*
	* @access public
	*/
	public function get_forum_data() {
		// execute the queries to get the information
		if(isset($this->forum_id)) {
			// this is a subforum
			$this->index_data = Db_Abstraction::query("SELECT * FROM `nbb_forums` WHERE `forum_parent` = '" . $this->forum_id . "'");
			$this->lates_data = Db_Abstraction::query("SELECT `post_time` AS `date`, `post_id`, `topic_id`, `nbb_forums`.`forum_id`, `post_title`, `user_id`, `user_name`, UNIX_TIMESTAMP(post_time) AS `unix_time` FROM `nbb_posts` LEFT JOIN `nbb_users` ON `nbb_users`.`user_id` = `nbb_posts`.`post_user_id` LEFT JOIN `nbb_forums` ON `nbb_posts`.`forum_id` = `nbb_forums`.`forum_id` WHERE `nbb_forums`.`forum_parent` = '" . $this->forum_id . "' GROUP BY `nbb_forums`.`forum_id`");
		} else {
			// standard forum index
			$this->index_data = Db_Abstraction::query("SELECT * FROM `nbb_forums`");
			$this->lates_data = Db_Abstraction::query("SELECT `post_time` AS `date`, `post_id`, `topic_id`, `forum_id`, `post_title`, `user_id`, `user_name`, UNIX_TIMESTAMP(post_time) AS `unix_time` FROM `nbb_posts` LEFT JOIN `nbb_users` ON `nbb_users`.`user_id` = `nbb_posts`.`post_user_id` GROUP BY `forum_id` ORDER BY `post_time` DESC, `post_id` DESC"); // this query needs to be optimised
		}
		
		// both need the statistics
		$this->stats_data = ForumSettings::getGlobalStatistics();
	}
	
	/**
	* Get global stats from the database
	*
	* @access public
	*/
	public function get_stats_array() {
		// loop through the stats query to extract the data
		$this->stats_array = array(); $i = 0; $i2 = 0;
		foreach($this->stats_data as $row) {
			// what is this data?
			if($row['obj_type'] == 1) {
				// is a forum stat
				$this->stats_array['forums'][$row['obj_name']] = array('data' => $row['obj_data']);
				$i++;
			} elseif($row['obj_type'] == 2 && !isset($this->forum_id)) {
				// if the forum_id is set then we don't need this board stat
				$this->stats_array['board'][$row['obj_name']] = array('data' => $row['obj_data']);
				$i2++;
			}
		}
	}
	
	/**
	* Get's specific stats
	*
	* @param int $id
	* @param int $type
	* @return array
	* @access public
	*/
	public function get_specific_stats_array($id, $type) {
		// we need to find our specific id
		switch($type) {
			case 'posts':
				return $this->stats_array['forums'][$id . '_posts']['data'];
				break;
			case 'topics':
				return $this->stats_array['forums'][$id . '_topics']['data'];
				break;
			default:
				return NULL;
				break;
		}
	}
	
	/**
	* Get latest posts
	*
	* @access public
	*/
	public function get_latest_posts_array() {
		// loop through the latest posts query and store it in an array
		$this->latest_array = array();
		while($row = DB_Abstraction::fetch_assoc($this->lates_data)) {
			// loop through the latest posts
			$this->latest_array[$row['forum_id']] = array('title' => $row['post_title'],
														  'unix_time' => $row['unix_time'],
														  'post_date' => $row['date'],
														  'topic_id' => $row['topic_id'],
														  'username' => $row['user_name'],
														  'user_id' => $row['user_id'],
														  'user_profile_url' => urlSystem::viewURL(2, array('user_id' => $row['user_id'],
																											'user_name' => $row['user_name'])));
		}
	}
	
	/**
	* Generates the forums list
	*
	* @return array
	* @access public
	*/
	public function generate_forum_list() {
		// loop through the forums and generate an array
		$loop = array(); $i = 0;
		while($row = DB_Abstraction::fetch_assoc($this->index_data)) {
			// see what this row is
			switch($row['forum_type']) {
				case 0:
					// is a category
					if($i == 0) { $fcat = true; } else { $fcat = false; }
					$loop[$row['forum_id']] = array('name' => $row['forum_name'], 
													'first_cat' => $fcat,
													'is_cat' => true,
													'forums' => array(),
													'forum' => self::get_var('FORUM', 3),
													'l_post' => self::get_var('L_POST', 3),
													'topics_posts' => self::get_var('TOPICS_POSTS', 3));
					break;
				case 1:
					// is a forum
					// are global posts set?
					if(!isset($this->stats_array['forums'][$row['forum_id'] . '_posts']['data'])) {
						$total_posts = 0;
					} else {
						$total_posts = $this->stats_array['forums'][$row['forum_id'] . '_posts']['data'];
					}
					
					// are global topics set?
					if(!isset($this->stats_array['forums'][$row['forum_id'] . '_topics']['data'])) {
						$total_topics = 0;
					} else {
						$total_topics = $this->stats_array['forums'][$row['forum_id'] . '_topics']['data'];
					}
					
					// do we have any read/unread posts
					if(CoreUserSystem::isLoggedIn()) {
						if(isset($_SESSION['User']['tracking_table'][$row['forum_id']]['marked_read']) && $_SESSION['User']['tracking_table'][$row['forum_id']]['marked_read'] > $this->latest_array[$row['forum_id']]['unix_time']) {
							// no new posts
							$new_posts = false;
						} else {
							// marked read hasn't been set
							// is the last login timestamp more than last post
							if($_SESSION['User']['user_la1'] > $this->latest_array[$row['forum_id']]['unix_time']) {
								// no new posts
								$new_posts = false;
							} else {
								// there is
								$new_posts = true;
							}
						}
					} else {
						// we're not keeping track of guests, don't need or want to
						$new_posts = false;
					}
					
					// build the array
					$loop[$row['forum_parent']]['forums'][$row['forum_id']] = array(
							'f_id' => $row['forum_id'], 
							'f_desc' => $row['forum_desc'], 
							'f_name' => $row['forum_name'], 
							't_total_posts' => $total_posts, 
							't_total_topics' => $total_topics, 
							'n_posts' => self::get_var('N_POSTS', 3), 
							'w_threads' => self::get_var('S_THREADS', 1), 
							'w_posts' => self::get_var('S_POSTS', 1),
							'posts' => self::get_var('POSTS', 3),
							'new_posts' => $new_posts,
							'forum_url' => urlSystem::viewURL(1, array('forum_id' => $row['forum_id'])), 
							'latest_post_time' => $this->latest_array[$row['forum_id']]['unix_time'], 
							'latest_post_ptime' => date('G:ia', $this->latest_array[$row['forum_id']]['unix_time']),
							'latest_post_user' => $this->latest_array[$row['forum_id']]['username'], 
							'latest_post_title' => $this->latest_array[$row['forum_id']]['title'], 
							'latest_post_date' => date('d/m/Y', $this->latest_array[$row['forum_id']]['unix_time']),
							'latest_post_user_url' => $this->latest_array[$row['forum_id']]['user_profile_url'], 
							'latest_post_topic_url' => urlSystem::viewURL(3, array('topic_id' => $this->latest_array[$row['forum_id']]['topic_id'])), 
							'w_at' => self::get_var('AT', 1));
					
					// add to the global stats
					$overall_t_posts += $total_posts;
					$overall_t_topics += $total_topics;
					
					// end
					break;
				case 2:
					// is a subforum
					if(isset($this->forum_id)) {
						// we need some extra information here to build the proper subforum page
						// are global posts set?
						if(!isset($this->stats_array['forums'][$row['forum_id'] . '_posts']['data'])) {
							$total_posts = 0;
						} else {
							$total_posts = $this->stats_array['forums'][$row['forum_id'] . '_posts']['data'];
						}
						
						// are global topics set?
						if(!isset($this->stats_array['forums'][$row['forum_id'] . '_topics']['data'])) {
							$total_topics = 0;
						} else {
							$total_topics = $this->stats_array['forums'][$row['forum_id'] . '_topics']['data'];
						}
						
						$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['subforums'][$row['forum_id']] = array(
								'sf_name' => $row['forum_name'], 
								'sf_forum_url' => urlSystem::viewURL(1, array('forum_id' => $row['forum_id'])), 
								'total_posts' => $total_posts, 
								'total_topics' => $total_topics, 
								'topics' => self::get_lang_var('TOPICS', 3), 
								'posts' => self::get_lang_var('POSTS', 3), 
								'sf_desc' => $row['forum_desc'], 
								'n_posts' => self::get_var('N_POSTS', 3), 
								'w_threads' => self::get_var('S_THREADS', 1), 
								'w_posts' => self::get_var('S_POSTS', 1),
								'latest_post_time' => $this->latest_array[$row['forum_id']]['unix_time'], 
								'latest_post_ptime' => date('G:ia', $this->latest_array[$row['forum_id']]['unix_time']),
								'latest_post_user' => $this->latest_array[$row['forum_id']]['username'], 
								'latest_post_title' => $this->latest_array[$row['forum_id']]['title'], 
								'latest_post_date' => date('d/m/Y', $this->latest_array[$row['forum_id']]['unix_time']),
								'latest_post_user_url' => $this->latest_array[$row['forum_id']]['user_profile_url'], 
								'latest_post_topic_url' => urlSystem::viewURL(3, array('topic_id' => $this->latest_array[$row['forum_id']]['topic_id'])), 
								'w_at' => self::get_var('AT', 1));
					} else {
						// build a subforum list for the main index
						if(!isset($loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['subforums'])) {
							// the first subforum, disable the ', '
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['subforums'][$row['forum_id']] = array('first_sf' => true, 'sf_name' => $row['forum_name'], 'sf_forum_url' => urlSystem::viewURL(1, array('forum_id' => $row['forum_id'])));
						} else {
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['subforums'][$row['forum_id']] = array('sf_name' => $row['forum_name'], 'sf_forum_url' => urlSystem::viewURL(1, array('forum_id' => $row['forum_id'])));
						}

						// update the parent's forum latest post
						if($loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_time'] < $this->latest_array[$row['forum_id']]['unix_time']) {
							// update the main forum
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_time'] = $this->latest_array[$row['forum_id']]['unix_time'];
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_ptime'] = date('G:ia', $this->latest_array[$row['forum_id']]['unix_time']);
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_date'] = date('d/m/Y', $this->latest_array[$row['forum_id']]['unix_time']);
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_user'] = $this->latest_array[$row['forum_id']]['username'];
							$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['latest_post_title'] = $this->latest_array[$row['forum_id']]['title'];
							
							// because this is a subforum, the parent forum needs updating incase there as unread posts in here
							// do we have any read/unread posts
							if(CoreUserSystem::isLoggedIn()) {
								if(isset($_SESSION['User']['tracking_table'][$row['forum_id']]['marked_read']) && $_SESSION['User']['tracking_table'][$row['forum_id']]['marked_read'] > $this->latest_array[$row['forum_id']]['unix_time']) {
									// no new posts
									$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['new_posts'] = false;
								} else {
									// marked read hasn't been set
									// is the last login timestamp more than last post
									if($_SESSION['User']['user_la1'] > $this->latest_array[$row['forum_id']]['unix_time']) {
										// no new posts
										$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['new_posts'] = false;
									} else {
										// there is
										$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['new_posts'] = true;
									}
								}
							} else {
								// we're not keeping track of guests, don't need or want to
								$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['new_posts'] = false;
							}
						}
						
						// add to the parent forum stats
						$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['t_total_posts'] += $this->stats_array['forums'][$row['forum_id'] . '_posts']['data'];
						$loop[$row['forum_cat_id']]['forums'][$row['forum_parent']]['t_total_topics'] += $this->stats_array['forums'][$row['forum_id'] . '_topics']['data'];
						
						// add to the global stats
						$this->extra_data['overall_posts'] += $this->stats_array['forums'][$row['forum_id'] . '_posts']['data'];
						$this->extra_data['overall_topics'] += $this->stats_array['forums'][$row['forum_id'] . '_topics']['data'];
						
					}
					
					// end
					break;
				case 3:
					// is a redirecting forum
					// build the array
					$loop[$row['forum_parent']]['forums'][$row['forum_id']] = array(
							'f_id' => $row['forum_id'], 
							'f_desc' => $row['forum_desc'], 
							'f_name' => $row['forum_name'], 
							'forum_url' => urlSystem::viewURL(1, array('forum_id' => $row['forum_id'])), 
							'redirecting' => true,
							'redir_url' => $row['forum_url']);
					
					// end
					break;
			}
			
			$i++;
		}

		$this->total_forums = $i;
		$loop[$i+1] = array('ending_block' => true);
		return $loop;
	}
	
	/**
	* get's a language variable
	*
	* @param string $name
	* @param int $int
	* @return string
	* @access public
	*/
	public function get_lang_var($name, $int) {
		// retrieve local language variables
		return self::get_var($name, $int);
	}
	
	/**
	* Get's specific stats
	*
	* @param string $szUsername
	* @return boolean
	* @access protected
	*/
	public function get_local_statistics($id) {
		// retrieve statistics about the forum
		switch($id) {
			case 0:
				// total forums
				return;
				break;
			case 1:
				// total members
				return $this->stats_array['board']['registered_members']['data'];
				break;
			case 2:
				// newest member
				return $this->stats_array['board']['newest_member']['data'];
				break;
			case 3:
				// overall topics
				return $this->extra_data['overall_topics'];
				break;
			case 4:
				// overall posts
				return $this->extra_data['overall_posts'];
				break;
		}
		
	}

}

?>